Skip to content

[Clippy] Address clippy::unnecessary_cast in production code#1238

Draft
DanielEScherzer wants to merge 3 commits into
rust-lang:mainfrom
DanielEScherzer:clippy-unnecessary_cast
Draft

[Clippy] Address clippy::unnecessary_cast in production code#1238
DanielEScherzer wants to merge 3 commits into
rust-lang:mainfrom
DanielEScherzer:clippy-unnecessary_cast

Conversation

@DanielEScherzer

@DanielEScherzer DanielEScherzer commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

In a lot of places, the lint is triggered when casting a constant from the libgit2 bindings that is created (in libgit2-sys) via git_enum!. That macro will use i32 when #[cfg(target_env = "msvc")], and u32 otherwise, meaning that a number of the casts that clippy flags as "unnecessary" are indeed necessary. In those cases, use #[allow] with a reason.

Similarly, clippy also lints against casting of c_int and c_uint to i32 and u32 - while those are usually the same types, the libc documentation makes it clear that they are not always the same. In those cases too, use #[allow] with a reason.

The only libc-provided type with a consistent rust type is that size_t is always the same as usize - fix the places that clippy identified where that conversion is unneeded. Other fixes include casts when the underlying type is explicitly set in the libgit2 bindings to be a specific rust type, or when converting mutable pointers to the same type they already are.

@rustbot rustbot added the S-waiting-on-review Status: Waiting on review label Apr 22, 2026
@DanielEScherzer

Copy link
Copy Markdown
Contributor Author

This is the first of a few PRs to get clippy passing, and then I'll send a PR to add it to CI

Comment thread src/apply.rs
@rustbot

This comment has been minimized.

Comment thread src/apply.rs
DanielEScherzer added a commit to DanielEScherzer/git2-rs that referenced this pull request Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of
violations. The existing violations are allowlisted on a module level (i.e. at
the top of files with inner attributes, or above the start of nested inline
test modules with outer attributes).

CI runs clippy on both Window and Linux because some lints may fire on one but
not the other; see, e.g. rust-lang#1238.

Existing violations will be addressed in subsequent pull requests.
DanielEScherzer added a commit to DanielEScherzer/git2-rs that referenced this pull request Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of
violations. The existing violations are allowlisted on a module level (i.e. at
the top of files with inner attributes, or above the start of nested inline
test modules with outer attributes).

CI runs clippy on both Window and Linux because some lints may fire on one but
not the other; see, e.g. rust-lang#1238.

Existing violations will be addressed in subsequent pull requests.
DanielEScherzer added a commit to DanielEScherzer/git2-rs that referenced this pull request Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of
violations. The existing violations are allowlisted on a module level (i.e. at
the top of files with inner attributes, or above the start of nested inline
test modules with outer attributes).

CI runs clippy on both Window and Linux because some lints may fire on one but
not the other; see, e.g. rust-lang#1238.

Existing violations will be addressed in subsequent pull requests.
DanielEScherzer added a commit to DanielEScherzer/git2-rs that referenced this pull request Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of
violations. The existing violations are allowlisted on a module level (i.e. at
the top of files with inner attributes, or above the start of nested inline
test modules with outer attributes).

CI runs clippy on both Window and Linux because some lints may fire on one but
not the other; see, e.g. rust-lang#1238.

Existing violations will be addressed in subsequent pull requests.
DanielEScherzer added a commit to DanielEScherzer/git2-rs that referenced this pull request Jun 15, 2026
Add clippy to CI so that future changes do not increase the number of
violations. The existing violations are allowlisted on a module level (i.e. at
the top of files with inner attributes, or above the start of nested inline
test modules with outer attributes).

CI runs clippy on both Window and Linux because some lints may fire on one but
not the other; see, e.g. rust-lang#1238.

Existing violations will be addressed in subsequent pull requests.
@DanielEScherzer
DanielEScherzer marked this pull request as draft June 15, 2026 18:20
@rustbot rustbot removed the S-waiting-on-review Status: Waiting on review label Jun 15, 2026
@DanielEScherzer
DanielEScherzer force-pushed the clippy-unnecessary_cast branch from 66ebc7d to 08e5e75 Compare July 19, 2026 20:01
@DanielEScherzer DanielEScherzer changed the title Clippy: allow or fix clippy::unnecessary_cast lint in src/ [Clippy] Address clippy::unnecessary_cast in production code Jul 19, 2026
In a lot of places, the lint is triggered when casting a constant from the
libgit2 bindings that is created (in libgit2-sys) via `git_enum!`. That macro
will use i32 when `#[cfg(target_env = "msvc")]`, and u32 otherwise, meaning
that a number of the casts that clippy flags as "unnecessary" are indeed
necessary. In those cases, use `#[allow]` with a reason.

Similarly, clippy also lints against casting of `c_int` and `c_uint` to `i32`
and `u32` - while those are usually the same types, the libc documentation
makes it clear that they are not always the same. In those cases too, use
`#[allow]` with a reason.

The only libc-provided type with a consistent rust type is that `size_t` is
always the same as `usize` - fix the places that clippy identified where that
conversion is unneeded. Other fixes include casts when the underlying type is
explicitly set in the libgit2 bindings to be a specific rust type, or when
converting mutable pointers to the same type they already are.
@DanielEScherzer
DanielEScherzer force-pushed the clippy-unnecessary_cast branch from 08e5e75 to 4e982c7 Compare July 19, 2026 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants